<?php include('../resources/views/header.blade.php'); ?>

<section>
	<div class="item" style="width:96%;margin:0 auto;background-color:#58bdfd;padding:2px 4px;border-radius:1px;margin-top:12px;">
        <div style="height:42px;background-color:#fff;border-radius:1px;overflow:hidden;">
            <img src="images/search_icon.png" id="search_btn" style="width:30px;height:30px;float:right;margin-top:4px;" />
            <input id="search_input" style="padding:0;height:100%;border-style:none;margin-left:4px;margin-right:30px;border-radius:0px;outline:none;font-size:18px;" <?php if(isset($content)){ echo 'value="'.$content.'"'; } ?> placeholder="搜索问题" />
        </div>
    </div>
</section>


<section style="margin-top:12px;background-color:#fff;margin-bottom:-12px;" id="result_wrap">
    <?php
        if( isset($questions) ){
            foreach( $questions as $key => $val ){
                echo '<a href="/detail/'.$val->id.'">';
                echo '<div style="padding-top:12px;padding-bottom:12px;padding-left:16px;padding-right:16px;">';
            	echo '	<p style="margin:0;font-size:16px;color:#333;">'.$val->title.'</p>';
            	echo '	<div style="margin-top:5px;">';
                $temp_answers_count = isset($answers_count[$val->id]) ? $answers_count[$val->id] : 0;
            	echo '		<span style="color:#888;font-size:12px;">回答 '.$temp_answers_count.'</span>';
                $temp_attentions_count = isset($attentions_count[$val->id]) ? $attentions_count[$val->id] : 0;
            	echo '		<span style="margin-left:12px;color:#888;font-size:12px;">关注 '.$temp_attentions_count.'</span>';
            	echo '	</div>';
            	echo '</div>';
                echo '</a>';
                if( $key < count($questions)-1 ){
                    echo '<div style="height:1px;background-color:#eee;margin-left:16px;margin-right:16px;"></div>';
                }
            }
        }
    ?>
</section>

<?php include('../resources/views/footer.blade.php'); ?>



<script type="text/javascript">
    $("#search_btn").on('click',function(){
        var content       = $("#search_input").val();
        if( content == '' ){
            toast('搜索内容不能为空');
        }else{
            $.ajax({
                type: "get" ,
                url: "/search?content="+content+"&type=ajax" ,
                success : function(data){
                    console.log(data);
                    if(!data.error ){
                        var temp_str='';
                        for(var i=0;i<data.result.questions.length;i++){
                            var question_id = data.result.questions[i].id;
                            var answer = data.result.answers_count[question_id] == undefined ? 0 : data.result.answers_count[question_id];
                            var attention = data.result.attentions_count[question_id] == undefined ? 0 : data.result.attentions_count[question_id];
                            temp_str += "<a href='/detail/"+question_id+"'>\
                                            <div style='padding-top:12px;padding-bottom:12px;padding-left:16px;padding-right:16px;'>\
                                                <p style='margin:0;font-size:16px;color:#333;'>"+data.result.questions[i].title+"</p>\
                                                <div style='margin-top:5px;'>\
                                                    <span style='color:#888;font-size:12px;'>回答 "+answer+"</span>\
                                                    <span style='margin-left:12px;color:#888;font-size:12px;'>关注 "+attention+"</span>\
                                                </div>\
                                            </div>\
                                        </a>\
                                        ";
                        }
                        $("#result_wrap").html(temp_str);
                    }else{
                        toast(data.message);
                    }
                }
            });
        }
    });
</script>
